package com.xgame.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xgame.mybatis.model.User;
import com.xgame.poto.BasePacket.MsgCode;
import com.xgame.poto.BasePacket.Packet;
import com.xgame.poto.UserProto.OnlineUserPb;
import com.xgame.poto.UserProto.UserPb;
import com.xgame.service.UserService;
import com.xgame.util.ProtobufResponseUtil;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService;

	/**
	 * 进入登录平台接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public Packet login(@RequestBody UserPb userPb) {
		Packet.Builder basePacket = Packet.newBuilder();
		basePacket.setState(1);
		if (log.isDebugEnabled()) {
			log.debug("******login**************");
		}
		try {
			User user = userService.findUserByName(userPb.getUserName());
			if (userPb.getLoginType() == 2) {// 注册
				if (user == null) {
					user = new User();
					user.setUserName(userPb.getUserName());
					user.setNickName(userPb.getUserName());
					String password = getESPlus().encrypt(userPb.getPassword());
					user.setPassword(password);
					userService.add(user);
				} else {
					basePacket.setState(0);
					basePacket.setMsg("用户已存在");
				}
			} else {// 登录
				if (getESPlus().decrypt(user.getPassword()).equals(
						userPb.getPassword())) {
					// 验证成功
					UserPb.Builder userPb2 = (UserPb.Builder) ProtobufResponseUtil
							.protobufResponseValue(user, userPb);
					basePacket.setData(userPb2.build().toByteString());
					OnlineUserPb.Builder onlineUserPb = OnlineUserPb
							.newBuilder();
					getRequest().getSession().setAttribute("ouser",
							onlineUserPb);
					// basePacket.setMsgType(311);
					basePacket.setState(1);
					basePacket.setMsgCode(MsgCode.loginok);
					// basePacket.setMsg("登录成功");
				} else {
					// 失败
					basePacket.setState(0);
					basePacket.setMsgCode(MsgCode.pwdErr);
					basePacket.setMsg("密码错误，验证失败");
				}
			}
		} catch (Exception e) {
			// e.printStackTrace();
			log.error(e.getLocalizedMessage(), e);
			basePacket.setMsg("系统错误" + e.getLocalizedMessage());
		}
		return basePacket.build();
	}

	@RequestMapping(value = "/get", method = RequestMethod.GET)
	@ResponseBody
	public Packet.Builder get() {
		Packet.Builder basePacket = Packet.newBuilder();
		basePacket.setState(1);
		basePacket.setState(0);
		basePacket.setMsg("用户已存在");
		return basePacket;
	}

	@RequestMapping(value = "/test", method = RequestMethod.GET)
	@ResponseBody
	public String t() {
		return "test";
	}
}
